<?php

namespace app\modules\mall\models;
use Yii;
use yii\db\ActiveRecord;
use yii\db\Query;

/**
 * 众测福利社
 * @author XT
 *
 */
Class Welfare extends ActiveRecord {
    
    const YY_WELFARE_PARTICIPANTS   = 'yy_welfare_participants';
    const YY_WELFARE_COMMENT        = 'yy_welfare_comment';
    
    public static function db() {
        return Yii::$app->mall;
    }
    
    public static function tableName() {
        return 'yy_welfare';
    }
    
    public static function query() {
        return new Query();
    }
    
    /**
     * 得到众测福利社首页列表
     */
    public function index() {
        $time = time();
        return self::query()->select('id, goodsid, picid')
        ->from(self::tableName())
        ->where(['and', ['<', 'begintime', $time], ['>', 'endtime', $time], ['=', 'is_show', 1], ['=', 'is_end', 1]])
        ->orderBy(['sort' => SORT_ASC])
        ->all(self::db());
    }
    
    /**
     * 得到众测福利社商品信息
     */
    public function detail($goodsid) {
        $time = time();
        return self::query()->select('id, pricenum, endtime, condition')
        ->from(self::tableName())
        ->where(['and', ['goodsid' => $goodsid], ['<', 'begintime', $time], ['>', 'endtime', $time], ['is_show' => 1], ['is_end' => 1]])
        ->one(self::db());
    }
    /**
     * 通过福利社活动id得到参加某福利社活动人数
     * @param unknown $wid
     */
    public function getJoinNum($wid) {
        return self::query()
            ->from(self::YY_WELFARE_PARTICIPANTS)
            ->where(['wid'=>$wid])
            ->count('*', self::db());
    }
    /**
     * 返回中奖人数
     */
    public function lucknum($wid)
    {
        return self::query()
        ->from(self::YY_WELFARE_PARTICIPANTS)
        ->where(['and',['=','wid',$wid],['=','is_lucky',1]])
        ->count('*', self::db());
    }
    /**
     * 通过福利社活动id获取福利社活动信息
     * @param unknown $wid
     */
    public function getInfo($wid) {
        return self::query()->select('condition ,winner, pricenum, probability')->from(self::tableName())
        ->where(['=','id',$wid])->one(self::db());
    }
    /**
     * 返回用户抽奖次数
     */
    public function usertimes($uid,$wid) {
        return self::query()->from(self::YY_WELFARE_PARTICIPANTS)
        ->where(['and', ['=', 'uid', $uid], ['=', 'wid', $wid]])
        ->count('*', self::db());
    }
    /**
     * 写入用户是否中奖
     * */
    public function insertuser($data)
    {
        return self::db()->createCommand()->insert(self::YY_WELFARE_PARTICIPANTS, array(
            'uid'=>$data['uid'],
            'is_lucky'=>$data['is_lucky'],
            'wid'=>$data['wid'],
            'createtime' => $data['createtime'],
        ))->execute();
    }
    /**
     * 同意福利社协议
     * @param unknown $data
     */
    public function agreeProc($data) {
        return self::db()->createCommand()
        ->update(self::YY_WELFARE_PARTICIPANTS, [
            'is_agree' => $data['is_agree']
        ], [
            'uid' => $data['uid'],
            'wid' => $data['wid'],
            'is_lucky' => 1,
        ])->execute();
    }
    /**
     * 更新用户参加福利社活动状态
     * @param unknown $data
     */
    public function updateState($data) {
       return self::db()->createCommand()
       ->update(self::YY_WELFARE_PARTICIPANTS, [
           'is_lucky' => 0
       ], [
           'uid' => $data['uid'],
           'wid' => $data['wid'],
           'is_lucky' => 1,
       ])->execute();
    }
    /**
     * 判断福利社活动是否存在
     * @param unknown $wid
     */
    public function isExist($wid) {
        return self::query()->from(self::tableName())
        ->where(['=', 'id', $wid])
        ->count('*', self::db());
    }
    /**
     * 得到福利社商品的官方评论
     * @param unknown $goodsid
     */
    public function getComment($goodsid) {
        return self::query()->select('content, cid')
        ->from(self::YY_WELFARE_COMMENT)
        ->where(['=', 'goodsid', $goodsid])
        ->one(self::db());
    }
} 